<?php
	class User_model extends Model{
		function User_model(){
			parent::Model();
		}
		/*
		*	Queries the database to get the surveys created by a user.
		*	Parameters:
		*	$user_id -> tells which user should we get the surveys
		*/
		function get_my_surveys($user_id){
			$sql = "SELECT * FROM survey WHERE user_id = $user_id";
			$query = $this->db->query($sql);
			return $query;
		}
		
		/*
		 * Queries the database to get the title of the survey given the survey id
		 *
		 * Parameter:
		 * $survey_id -> indicates which survey you want to get the title of
		 *
		 * Return Value:
		 * returns the title of the survey
		 */
		function get_survey_title($survey_id)
		{
			$sql = "SELECT * FROM survey
					WHERE survey_id = $survey_id";
			$query = $this->db->query($sql);
			return $query->row(0)->title;
		}

		/*
		*	Gets the total number of questions in a survey
		*	Parameters:
		*	$survey_id - the survey that we want to count the number of questions
		*/		
		function get_number_of_questions($survey_id){
			$user_id = $this->session->userdata('id');
			$sql = "SELECT count(*) as counted FROM question
					WHERE survey_id = $survey_id";
			$query = $this->db->query($sql);
			if ($query->num_rows == 0){
				return 0;
			}
			else{
				return $query->row()->counted;
			}
		}
		/*
		*	Inserts the choices that belongs to a certain question.
		*	Parameters:
		* 	$new - an array that contains the data values of the choices.
		*/
		function insert_choice($new){
			$this->db->insert('choices',$new);
		}
		/*
		*	inserts the data about the questions to the database.
		*	Parameters:
		*	$toInsert - an array that contains all the information  about the question.
		*/
		function insert_questions_data($toInsert){
			$this->db->insert('question',$toInsert);
		}
		/*
		*	Permanently deletes a survey including the choices and data about it.
		*	Parameters
		*	$toDelete - an array of surveys that are to be deleted.
		*/
		function delete_survey_from_db($toDelete){//survey ang dinedelete
			$limit = count($toDelete);
			for ($i = 0; $i < $limit; $i++){
				$delete = $toDelete[$i];
				$survey = "DELETE FROM survey
						WHERE survey_id = $delete";
				$req_fields = "DELETE FROM required_fields
						WHERE survey_id = $delete";
				$question = "DELETE FROM question
						WHERE survey_id = $delete";
				$invitation = "DELETE FROM invitation
						WHERE survey_id = $delete";
				$choices = "DELETE FROM choices
						WHERE survey_number = $delete";
				$answer = "DELETE FROM answer
						WHERE survey_id = $delete";
				$this->db->query($survey);
				$this->db->query($req_fields);
				$this->db->query($question);
				$this->db->query($invitation);
				$this->db->query($choices);
				$this->db->query($answer);
			}
		}
		/*
		*	Deletes questions belonging to a survey.
		* 	Parameters
		*	$toDelete - an array of questions that are selected for deletion
		*	$survey_id - the survey that contains the questions to be deleted.
		*/
		function delete_from_db($toDelete,$survey_id){
			$N = count($toDelete);
			
			for($i = 0; $i < $N; $i++){
				
				$delete = $toDelete[$i];
				$sql = "DELETE FROM question
						WHERE survey_id = $survey_id
						AND question_number = $delete";
				$this->db->query($sql);
				
				$sql2 = "DELETE FROM choices
						WHERE survey_number = $survey_id
						AND question_number = $delete";
				$this->db->query($sql2);	
			}
		}
		
		/*
		 * Updates the question numbers, if ever a deletion of question is executed
		 *
		 * Parameter:
		 * $prev_q_num -> indicates the previous question number of the question to be updated
		 * $next_q_num -> indicates what question number will we update the desired question
		 * $survey_id -> indicates what survey does the question belong to
		 */
		function update_numbering($prev_q_num,$next_q_num,$survey_id)
		{
			$sql = "UPDATE question SET question_number = $next_q_num
					WHERE survey_id = $survey_id
					AND question_number = $prev_q_num";
			$this->db->query($sql);
			
			$sql2 = "UPDATE choices SET question_number = $next_q_num
					WHERE survey_number = $survey_id
					AND question_number = $prev_q_num";
			$this->db->query($sql2);			
		}

		
		/*
		*	Gets the choices of a question belonging to a certain survey
		*	Parameters:
		*	$survey_id - the survey that contains the question
		*	$question_number - the number of the question that we want to get
		
		*	Return value:
		*	$query - the choices of the question that we accessed
		*/
		
		function get_choices($survey_id,$question_number){
			$sql = "SELECT survey_number,choices.question_number,choice_number,choices.body FROM choices 
					WHERE survey_number = $survey_id
					AND choices.question_number = $question_number";
			$query = $this->db->query($sql);
			//echo $query->num_rows;
			return $query;
		}

		/*
		 * Queries the database to get the data from the answers table
		 * given the survey id and question number
		 *
		 * Parameters:
		 * $survey_id -> indicates what survey does the answer belong to
		 * $question_number -> indicates what question does the answer belong to
		 *
		 * Return Value
		 * $query -> the result set of the executed query
		 */
		function get_text_answers($survey_id, $question_number)
		{
			$sql = "SELECT * FROM answer
					WHERE survey_id = $survey_id
					AND question_number = $question_number";
			$query = $this->db->query($sql);
			return $query;
		}
		/*
		*	Gets the questions  belonging to a survey
		*	Parameters
		*	$survey_id - the id of the survey that where we will get the survey.
		*
		*	Return Value:
		*	$query - the questions that we will refer to
		*/
		function get_questions($survey_id){
			$sql = "SELECT * FROM question WHERE survey_id = $survey_id ORDER BY question_number";
			$query =$this->db->query($sql);
			//echo $query->num_rows;
			return $query;
		}
		/*
		*	 Gets a certain question from the database identified by its question number.
		*	 Parameters:
		*	$survey_id - the id of the survey that contains the question.
		*	$question_number - the number of the question that we want to get.
		*	return value:
		*	$query - the single question that we want to access.
		*/
		function get_a_question($survey_id, $question_number)
		{
			$sql = "SELECT * FROM question
					WHERE survey_id = $survey_id
					AND question_number = $question_number";
			$query = $this->db->query($sql);
			return $query;
		}
		
		/*
		 * Updates the question, given it's new values
		 *
		 * Parameters:
		 * $survey_id -> indicates what survey does the question belong to
		 * $question_number -> indicates what question are we updating
		 * $body, $type, $with_text, $text_label, $required, $choice_limit, $null, $category_name
		 *			-> the new data for the question
		 */
		function update_question($survey_id, $question_number, $body, $type, $with_text, $text_label, $required,$choice_limit,$null,$category_name)
		{
			//deletes first all choices  for the given question
			$sql = "DELETE FROM choices
					WHERE survey_number = $survey_id
					AND question_number = $question_number";
			$this->db->query($sql);
			
			$sql2 = "UPDATE question
					SET body = '$body',
						type = '$type',
						with_text = $with_text,
						text_label = '$text_label',
						required = $required,
						choice_limit = $choice_limit,
						null_field = $null,
						category_name = '$category_name'
					WHERE survey_id = $survey_id
					AND question_number = $question_number";
			$this->db->query($sql2);
		}
		function get_question_types($survey_id){
			$this->db->from('type');
			$query = $this->db->get();
			return $query->result_array();
		}
		/*
		*	Inserts the required fields of a survey.
		*	Parameters
		*	$survey_id - the id of the survey that we will insert the known data
		*	$firstname - indicates if firstname is required
		*	$middlename- indicates if middlename is required
		*	$lastname - indicates if lastname is required.
		*	$gender - indicates if gender is required.
		*	$birthday - indicates if birthday is required
		*	$location - indicates if location is required.
		*/
		function insert_required($survey_id,$firstname,$middlename,$lastname,$gender,$birthday,$location){
			$sql_required = "INSERT INTO required_fields
								(survey_id,firstname,middlename,lastname,gender,birthday,location)
								VALUES($survey_id,'$firstname','$middlename','$lastname','$gender','$birthday','$location')";
			$this->db->query($sql_required);					
		}
		/*
		*	Inserts the essential data about the survey to the database.
		*	Parameters
		*	$survey_name - the name of the survey
		*	$description - the description of the survey
		*	$begin_month - the begin month to be concatenated later
		*	$begin_day - the start day of the survey to be concatenated later
		*	$begin year - the year of start of the survey to be concatenated later
		*	$end_month - the month when the survey will end to be concatenated later
		*	$end_day - the day that the survey will end to be concatenated later
		*	$end_year - the year that the survey will end. to be concatenated later
		*	$id - the id of the user
		*	$sched - the number of days for the notification schedule
		*	$notif_body -  the body of the notificatin
		*/
		function insert_survey($survey_name,$description,$begin_month,$begin_day,$begin_year,$end_month,$end_day,$end_year,$id,$sched, $notif_body){
			$date_created = date('y-m-d');
			$months = array('jan','feb','mar','april','may','june','july','august','sept','oct','nov','dec');
			for($i = 0; $i<12; $i++){
				if ($months[$i] == $begin_month){
					$begin_month = $i+1;
					//$i = 1;
					//echo $begin_month;
					break;				
				}
			}
			for($i = 0; $i<12; $i++){
				if ($months[$i] == $end_month){
					$end_month = $i+1;
					//$i = 1;
					//echo $end_month;
					break;				
				}
			}
			$begin_date = $begin_year.'-'.$begin_month.'-'.$begin_day;
			$end_date = $end_year.'-'.$end_month.'-'.$end_day;
			//echo $begin_date;
			//echo $end_date;

			//echo "$date_created";
			
			$sql_survey = "INSERT INTO survey 
							(title,description,date_created,begin_date,end_date,user_id,notif_sched, notif_body)
							VALUES('$survey_name','$description','$date_created','$begin_date','$end_date',$id,$sched, '$notif_body')";
			$query_survey = $this->db->query($sql_survey);
			
			$sql = "SELECT survey_id FROM survey
					WHERE title='$survey_name'
					AND description='$description'
					AND date_created = '$date_created'
					AND begin_date = '$begin_date'
					AND end_date = '$end_date'
					AND user_id = $id
					AND notif_body = '$notif_body'";
			
			$query = $this->db->query($sql);
			return $query->row()->survey_id;
		}
		/*
		*	Searches the database for a match of the survey that was created by a user.
		*	Parameters:
		*	$user_id - the id of the user that will be the basis of where to search
		*	$toSearch - the survey name that we want to search
		*	return value:
		*	$query - the survey that matches $toSearch may be null if no match is found.
		*/
		function search_survey($user_id,$toSearch){
			$sql_search = "SELECT * FROM survey
					WHERE title LIKE '%$toSearch%'
					AND user_id = $user_id";
			$query = $this->db->query($sql_search);		
			return $query;
		}
		/*
		*	Updates the essential data about the survey to the database.
		*	Parameters
		*	$survey_name - the name of the survey
		*	$description - the description of the survey
		*	$begin_month - the begin month to be concatenated later
		*	$begin_day - the start day of the survey to be concatenated later
		*	$begin year - the year of start of the survey to be concatenated later
		*	$end_month - the month when the survey will end to be concatenated later
		*	$end_day - the day that the survey will end to be concatenated later
		*	$end_year - the year that the survey will end. to be concatenated later
		*	$id - the id of the user
		*	$sched - the number of days for the notification schedule
		*	$notif_body -  the body of the notificatin
		*/
		function update_survey($survey_id,$title,$description,$begin_year,$begin_month,$begin_day,$end_year,$end_month,$end_day,$sched,$required,$toDelete, $notif_body){
			/*
			echo $begin_year,"<br>";
			echo $begin_month,"<br>";
			echo $begin_day,"<br>";
			
			echo $end_year,"<br>";
			echo $end_month,"<br>";
			echo $end_day,"<br>";
			*/
			$months = array('jan','feb','mar','april','may','june','july','august','sept','oct','nov','dec');
			for($i = 0; $i<12; $i++){
				if ($months[$i] == $begin_month){
					$begin_month = $i+1;
					//$i = 1;
					//echo $begin_month;
					break;				
				}
			}
			for($i = 0; $i<12; $i++){
				if ($months[$i] == $end_month){
					$end_month = $i+1;
					//$i = 1;
					//echo $end_month;
					break;				
				}
			}
			
			$N = count($toDelete);
			for($i = 0; $i < $N; $i++){
				$delete = $toDelete[$i];
				$sql = "DELETE FROM categories
						WHERE category_id = $delete";
				$sql_cat_name = "SELECT category_name FROM categories WHERE
									category_id = $delete";
				$query_cat_name = $this->db->query($sql_cat_name);
				$cat_name = $query_cat_name->row()->category_name;
				
				$sql_del_question = "DELETE FROM question 
									WHERE category_name = '$cat_name'
									AND survey_id = $survey_id";
				$this->db->query($sql_del_question);
				$this->db->query($sql);
			}
			
			
			$begin_date = $begin_year.'-'.$begin_month.'-'.$begin_day;
			//echo $begin_date,"wtf";
			$end_date = $end_year.'-'.$end_month.'-'.$end_day;
			$sql_update = "UPDATE survey
							SET title='$title',description = '$description', begin_date='$begin_date', end_date='$end_date', notif_sched = $sched, notif_body = '$notif_body' 
							WHERE survey_id = $survey_id";
							
			$firstname = 0;
			$middlename = 0;
			$lastname = 0;
			$gender = 0;
			$birthday = 0;
			$location = 0;
			if (!isset($required)){
			}
			else{
				for($i = 0; $i<count($required); $i++){
					if ($required[$i] == "firstname"){
						$firstname = 1;
					}
					else if ($required[$i] == "middlename"){
						$middlename = 1;
					}
					else if ($required[$i] == "lastname"){
						$lastname = 1;
					}
					else if ($required[$i] == "gender"){
						$gender = 1;
					}
					else if ($required[$i] == "location"){
						$location = 1;
					}
					else if ($required[$i] == "birthday"){
						$birthday = 1;
					}
				}				
			}
			$sql_update_req = "UPDATE required_fields
								SET firstname=$firstname,middlename=$middlename,lastname=$lastname,gender=$gender,location=$location,birthday=$birthday
								where survey_id=$survey_id";
			$this->db->query($sql_update);
			$this->db->query($sql_update_req);
		}
		/*
		*	Gets the questions according to their groups
		*	Parameters:
		*	$survey_id - the id of the survey where we will get the questions
		*	$category_id - the id of the category that we want to have as basis
		*	$category_name  - the exact name of the category
		*	Return value:
		*	$query - contains the questions belonging to a category
		*/
		function get_questions_grouped($survey_id,$category_id,$category_name){
			$sql = "SELECT * FROM question

					WHERE survey_id = $survey_id
					
					AND category_name = '$category_name'
					ORDER BY question_number";
			$query = $this->db->query($sql);
			//echo $query->num_rows;
			return $query;
		}
		/*
		*	Tests wheter a survey has a question group/categories
		*	Parameters:
		*	$survey_id - the id of the survey that we want to test
		*	Return value:
		*	true if there are groups
		*	false if none
		*/
		function has_groups($survey_id){
			$sql = "SELECT * FROM categories
					WHERE survey_id = $survey_id";
			$query = $this->db->query($sql);
			if($query->num_rows == 0){
				//echo "woooot";
				return false;
			}
			else{
				//echo "wth";
				return true;
			}
		}
		/*
		*	Gets the end date of a certain survey
		*	Parameters:
		*	$survey_id - the survey that we want to get the end date
		*	return value:
		*	the end date of the survey.
		*/
		function get_end_date($survey_id){
			$sql = "SELECT end_date FROM survey
					WHERE survey_id = $survey_id";
			$query = $this->db->query($sql);
			return $query->row()->end_date;
		}
		/*
		*	Forces a survey to end
		*	Parameters:
		*	$survey_id - the survey that we want to end
		*/
		function end_survey_now($survey_id){
			$today = date("Y-m-d");
			//echo $today;
			//echo $survey_id;
			$sql = "UPDATE survey
					SET end_date = '$today'
					WHERE survey_id = $survey_id";
			$this->db->query($sql);		
		}
		/*
		*	gets the categories of a survey
		*	Parameters
		*	$survey_id - the id of the survey that we want to get the categories.
		*	return value:
		*	$query - the categories of a survey. may contain zero rows if no categories found
		*/
		function get_categories($survey_id){
			$sql = "SELECT * FROM categories
					WHERE survey_id = $survey_id"; //try lng
			$query = $this->db->query($sql);
			return $query;
		}
		/*
		*	Inserts a category to the database.
		*	Parameters:
		*	$id - the id of the user
		*	$survey_id - the id of the survey where we will set the category
		*	$category_name - the name of the category.
		*	
		*/
		function insert_category($id,$survey_id,$category_name){
			$sql = "INSERT INTO categories
					(user_id,survey_id,category_name)
					VALUES($id,$survey_id,'$category_name')";
			$query = $this->db->query($sql);		
		}
		/*
		*	Updates the values of a category
		*	Parameters:
		*	$category_id - the id of the category that we want to update.
		*	$survey_id - the id of the survey that we will update
		*	$category_name - the new name for the category
		*	$old_cat_name - the old name of the category.
		*/
		function update_category($category_id,$survey_id,$category_name,$old_cat_name){
			$sql = "UPDATE categories
					SET category_name = '$category_name'
					WHERE category_id = $category_id";
			$query = $this->db->query($sql);
			
			$sql2 = "UPDATE question
					SET category_name = '$category_name'
					WHERE survey_id = $survey_id
					AND category_name = '$old_cat_name'";
			$query2 = $this->db->query($sql2);		
		}	
		
		/*
		 * Counts the number of answers for a given choice
		 *
		 * Parameters:
		 * $survey_id -> indicates what survey does the answer belong to
		 * $question_number -> indicates what question the answer is for
		 * $body -> indicates a choice of a question
		 *
		 * Return Value:
		 * the number of rows of the result set of the executed query
		 */
		function count_answer ($survey_id,$question_number,$body)
		{
			
			$sql = "SELECT * FROM answer
					WHERE survey_id = $survey_id
					AND question_number = $question_number
					AND body = '$body'";

			$query = $this->db->query($sql);
			return $query->num_rows;
		}
		
		/*
		 * Counts the number of answers for a given choice and for a given gender
		 *
		 * Parameters:
		 * $survey_id -> indicates what survey does the answer belong to
		 * $question_number -> indicates what question the answer is for
		 * $body -> indicates a choice of a question
		 * $gender -> indicates what gender are we considering
		 *
		 * Return Value:
		 * the number of rows of the result set of the executed query
		 */
		function count_answer_gender ($survey_id, $question_number, $body, $gender)
		{
			$sql =  "Select * FROM answer INNER JOIN respondent 
					ON answer.respondent_number = respondent.respondent_number 
					WHERE survey_id = $survey_id
					AND question_number = $question_number
					AND answer.body = '$body'
					AND respondent.gender = '$gender'";
			
			$query = $this->db->query($sql);
			return $query->num_rows;
		}
		
		/*
		 * Counts the number of answers for a given choice and for a given age range
		 *
		 * Parameters:
		 * $survey_id -> indicates what survey does the answer belong to
		 * $question_number -> indicates what question the answer is for
		 * $body -> indicates a choice of a question
		 * $date_upper -> indicates the upper bound of the birthdays we are considering
		 * $date_lower -> indicates the lower bound of the birthdays we are considering
		 *
		 * Return Value:
		 * the number of rows of the result set of the executed query
		 */
		function count_answer_age ($survey_id, $question_number, $body, $date_upper, $date_lower)
		{
			$sql =  "Select * FROM answer INNER JOIN respondent 
					ON answer.respondent_number = respondent.respondent_number 
					WHERE survey_id = $survey_id
					AND question_number = $question_number
					AND answer.body = '$body'
					AND respondent.birthday <= '$date_upper'
					AND respondent.birthday > '$date_lower'";
			
			$query = $this->db->query($sql);
			return $query->num_rows;
		}

		function count_answer_check($survey_id){
			$sql = "SELECT * FROM answer
					WHERE survey_id = $survey_id";
			$query = $this->db->query($sql);
			return $query->num_rows;		
		}
		/*
		 * Select a specific user on the table "user" based on their $id
		 * $id -> indicates the session userdata of the user.
		 * Returns the whole row of user_id = $id
		 */
		function select($id){
			$this->db->from('user');
			$this->db->where('user_id', $id);
			$query = $this->db->get();
			return $query->row_array();
		}
		
		function getCategoryCount($survey_id)
		{
			$this->db->select('group_name');
			$this->db->from('question_categories');
			$this->db->where('survey_id', $survey_id);
			$this->db->group_by('group_name');
			$result = $this->db->get()->result();
			return sizeof($result);
		}	
		
		function insertCategory($contents){
			$this->db->insert('question_categories', $contents);
		}
		/*
		 * This inserts subusers upon creation in the 'privileges' table.
		 * $content -> privileges granted (edit survey, update question, view reports, end survey)
		 */
		function insertSubUsers($content){
			$this->db->insert('privileges', $content);
		}
		/*
		 * Selects the subuser/s of the current user.
		 * $user_id -> indicates the session id userdata of the user.
		 */
		function getSubUser($user_id){
			$sql = "SELECT subuser, edit_questions, invite_respondents, view_reports, end_survey FROM privileges where superuser = $user_id";
			$query = $this->db->query($sql);
			return $query;
		}
		
		/*
		 * Checks whether the subuser a user is adding already exists on his list or not.
		 * $user_id -> indicates the session id userdata of the user.
		 * $email -> this is the main parameter used for checking.
		 */
		function alreadyASubuser($user_id, $email){
			$this->db->from('privileges');
			$this->db->where('superuser', $user_id);
			$this->db->where('subuser', $email);
			$query = $this->db->get();
			if($query->num_rows()==0){
				return FALSE;
			} else {
				return TRUE;
			}
		}

		/*
		 * Checks whether the a user's email being registered already exists in PollWatcher's DB.
		 * $email -> this is the main parameter used for checking.
		 */
		function alreadyAMember($email){
			$this->db->from('user');
			$this->db->where('email', $email);
			$query = $this->db->get();
			if($query->num_rows()==0){
				return FALSE;
			} else {
				return TRUE;
			}
		}

		/*
		 * Gets the number of subusers
		 * $user_id -> indicates the session id userdata of the user.
		 */
		function getNumSubUser($user_id){
			$sql = "SELECT subuser, edit_questions, invite_respondents, view_reports, end_survey FROM privileges where superuser = $user_id";
			$query = $this->db->query($sql);
			return $query->num_rows();
		}
		/*
		 * Gets the array of arrays of superuser, if exists.
		 * $useremail -> indicates the session email userdata of the user.
		 */
		function getSuperUser($useremail){
			$sql = "SELECT *  FROM privileges INNER JOIN user ON privileges.superuser = user.user_id WHERE privileges.subuser='$useremail'";
			$query = $this->db->query($sql);
			return $query;	
		}

		/*
		 * Updates privileges of subuser/s	
		 * $privileges -> array of privileges, being granted or taken away.
		 * $subuser -> subuser whose privileges are being edited.
		 */
		function update_privileges($privileges, $subuser){
			return $this->db->update('privileges', $privileges, array('subuser'=>$subuser));
		}

		function getImportantDates($user_id, $year, $month)
		{
			$where = "user_id = $user_id AND (begin_date LIKE '"."$year-$month-%"."' OR end_date LIKE '"."$year-$month-%"."' OR date_created LIKE '"."$year-$month-%"."')";
			$this->db->from('survey');
			$this->db->where($where);
			return $this->db->get()->result_array();
		}
		
		function getRespondentCount($survey_id)
		{
			$this->db->from('answer');
			$this->db->where('survey_id', $survey_id);		
			return sizeof($this->db->get()->result_array());
		}
		
		/*
		 * Gets privileges given by superusers
		 * $subuser_id -> in this case, the users session id data.
		 * $super_user_id -> id of your superuser.
		 */
		function get_privileges($subuser_id,$super_user_id){
			$sql = "SELECT * FROM privileges 
					INNER JOIN user
					ON user.email = privileges.subuser
					WHERE user.user_id = $subuser_id
					AND privileges.superuser = $super_user_id";
			$query = $this->db->query($sql);
			//echo $query->num_rows;
			return $query;
		}

		/*
		 * Checks whether the subuser a user is adding already exists on his list or not.
		 * $user_id -> indicates the session id userdata of the user.
		 * $email -> this is the main parameter used for checking.
		 */
		function isSubuser($subuser_id, $survey_id)
		{
			$this->db->select('user_id');
			$this->db->from('survey');
			$this->db->where('survey_id', $survey_id);
			$superuser_id = $this->db->get()->row_array();
			$superuser_id = $superuser_id['user_id'];
			
			$this->db->from('user');
			$this->db->where('user_id', $subuser_id);
			$subuser_email = $this->db->get()->row_array();			
			$subuser_email = $subuser_email['email'];
			
			$this->db->from('privileges');
			$this->db->where('subuser', $subuser_email);
			$this->db->where('superuser', $superuser_id);
			$subuser_id = $this->db->get()->row_array();
			
			if(sizeof($subuser_id) > 0)
			{
				return TRUE;
			}
			
			return FALSE;
		}

		/*
		 * Fetches the activation code of a new user user.
		 * $user_id -> indicates the session id userdata of the user.
		 */
		function get_email($user_id){
			$sql = "SELECT activation_code from user
					WHERE user_id = $user_id";
			$query = $this->db->query($sql);
			return $query->row()->activation_code;
			
		}
		
		/*
		 * Fetches the id of a SuperUser
		 * $survey_id -> the survey id that has granted privileges
		 */
		function getSuperuserId($survey_id)
		{
			$this->db->select('user_id');
			$this->db->from('survey');
			$this->db->where('survey_id', $survey_id);
			$superuser_id = $this->db->get()->row_array();
			return $superuser_id['user_id'];
		}
		function get_answers($survey_id, $question_number)
		{
			$sql = "SELECT * FROM answer
					WHERE survey_id = $survey_id
					AND question_number = $question_number";
					
			$query = $this->db->query($sql);
			return $query;
		}
		
		/*
		 * Gets the number of respondents who already answered the survey
		 *
		 * Parameters:
		 * $survey_id -> indicates what survey are we referring to
		 *
		 * Return Value:
		 * the number of rows of the result set of the executed query
		 */
		function number_respondents_answered($survey_id)
		{
			$sql = "Select * FROM invitation INNER JOIN respondent 
					ON invitation.respondent_number = respondent.respondent_number 
					WHERE survey_id = $survey_id
					AND respondent.status = 'done'";
			$query = $this->db->query($sql);
			return $query->num_rows;
		}
		
		/*
		 * Gets the number of invitations sent for a specific survey
		 *
		 * Parameters:
		 * $survey_id -> indicates what survey are we referring to
		 *
		 * Return Value:
		 * the number of rows of the result set of the executed query
		 */
		function number_invitations($survey_id)
		{
			$sql = "SELECT * FROM invitation
					WHERE survey_id = $survey_id";
			$query = $this->db->query($sql);
			return $query->num_rows;
		}
		
		/*
		 * Checks if gender belongs to one of the required fields of the survey
		 *
		 * Parameters:
		 * $survey_id -> indicates what survey are we referring to
		 *
		 * Return Value:
		 * true -> if gender is required
		 * false -> otherwise
		 */
		function is_gender_required ($survey_id)
		{
			$sql = "SELECT * FROM required_fields
					WHERE survey_id = $survey_id";
			$query = $this->db->query($sql);
			
			if ($query->row(0)->gender == 1)
			{
				return true;
			}else {
				return false;
			}
		}
		
		/*
		 * Checks if birthday belongs to one of the required fields of the survey
		 *
		 * Parameters:
		 * $survey_id -> indicates what survey are we referring to
		 *
		 * Return Value:
		 * true -> if gender is required
		 * false -> otherwise
		 */
		function is_age_required ($survey_id)
		{
			$sql = "SELECT * FROM required_fields
					WHERE survey_id = $survey_id";
			$query = $this->db->query($sql);
			
			if ($query->row(0)->birthday == 1)
			{
				return true;
			}else {
				return false;
			}
		}
		
		/*
		 * Fetches the row array of a user given user id.
		 * $user_id -> indicates the session id userdata of the user.
		 */
		function fetch($user_id)
		{
			$this->db->from('user');
			$this->db->where('user_id', $user_id);
			return $this->db->get()->row_array();
			
		}

	}
?>
